<template>
  <div class="page-wrap">
    <el-card shadow="never">
      <div class="toolbar">
        <el-input v-model="filters.search" placeholder="搜索名称/编码" clearable class="w-300" />
      </div>
      <el-table :data="dataList" border stripe>
        <el-table-column prop="code" label="编码" width="120" />
        <el-table-column prop="name" label="名称" min-width="160" />
        <el-table-column prop="warehouse" label="仓库" width="120" />
        <el-table-column prop="quantity" label="库存" width="100" />
        <el-table-column label="操作" width="160" fixed="right">
          <template #default="{ row }">
            <el-button size="small" @click="openTransfer(row)">调拨</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="visible" title="创建调拨" width="520px">
      <el-form :model="form" label-width="100px">
        <el-form-item label="商品"><span>{{ currentRow?.name }}（{{ currentRow?.code }}）</span></el-form-item>
        <el-form-item label="源仓库"><el-input v-model="form.from" placeholder="如：主仓库" /></el-form-item>
        <el-form-item label="目标仓库"><el-input v-model="form.to" placeholder="如：分仓库A" /></el-form-item>
        <el-form-item label="数量"><el-input-number v-model="form.quantity" :min="1" /></el-form-item>
        <el-form-item label="备注"><el-input v-model="form.remark" type="textarea" /></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="visible=false">取消</el-button>
        <el-button type="primary" @click="onTransfer">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref, computed, onMounted } from 'vue'
import { useStockStore } from '@/stores/stock.js'
const stock = useStockStore()
const filters = reactive({ search: '' })
const dataList = computed(()=> stock.getFilteredStock(filters))
onMounted(()=>{ if(!stock.stockList.length) stock.initializeStock() })

const visible = ref(false)
const currentRow = ref(null)
const form = reactive({ from: '', to: '', quantity: 1, remark: '' })

function openTransfer(row){ currentRow.value = row; form.from=row.warehouse||''; form.to=''; form.quantity=1; form.remark=''; visible.value=true }
function onTransfer(){
  if(!currentRow.value) return
  if(form.quantity > currentRow.value.quantity) return
  // 简化：直接减少源库存并在目标仓生成新增或累积
  stock.updateProduct(currentRow.value.id, { quantity: currentRow.value.quantity - form.quantity, warehouse: form.from })
  const target = stock.stockList.find(x=> x.code===currentRow.value.code && x.warehouse===form.to)
  if(target){
    stock.updateProduct(target.id, { quantity: target.quantity + form.quantity })
  } else {
    stock.addProduct({
      code: currentRow.value.code,
      name: currentRow.value.name,
      category: currentRow.value.category,
      specification: currentRow.value.specification,
      unit: currentRow.value.unit,
      quantity: form.quantity,
      minStock: currentRow.value.minStock,
      maxStock: currentRow.value.maxStock,
      costPrice: currentRow.value.costPrice,
      salePrice: currentRow.value.salePrice,
      warehouse: form.to,
      supplier: currentRow.value.supplier,
    })
  }
  visible.value=false
}
</script>

<style scoped>
.page-wrap { padding: 16px; }
.toolbar { display: flex; gap: 12px; margin-bottom: 12px; align-items: center; }
.w-300 { width: 300px; }
</style>


